/**
 * Author(s): xuming.Wong <xuming.Wong@gmail.com>
 */
#include "LoginScene.h"

#include "ClientConfig.h"
#include "GlobalVariables.h"
#include "NetManager.h"
#include "UISystem.h"

#include "CameraObject.h"
#include "Unit.h"


namespace PQClient
{
	LoginScene::LoginScene() : GameScene(Scene_Login),
		_status(Login_Select_Server),_relogin(false)
	{
		loadConfig();
	}

	void LoginScene::loadConfig()
	{
		//
		//load  server Addr  server Port info
		
		//save server  Ip address and port
		_serverAddr=Globals.getString(String_Server_IP);
		_serverPort=Globals.getInt32(Int32_Server_Port);

	}

	void LoginScene::loadScene()
	{
		__super::loadScene("Login.scene.xml");

		Unit* unit=getUnit("Destroyer");
		unit->setVisible(true);
		setActiveUnit(unit);
	}



	void LoginScene::update(float deltaT)
	{
		//update the scene
		GameScene::update(deltaT);


		//update login related 
		//updateNetwork();
		//update login related 

		//updateStatus();
	}

	void LoginScene::updateStatus()
	{
		switch(_status){
		case Login_Select_Server:
		{
			this->setActiveSceneId(Scene_ServerChange);
			break;
		}
		case Login_Debug_Setting:
			setStatus(Login_Disconnect);
			break;
		case Login_Disconnect:
			setStatus(Login_Connecting);
			NetManager::getSingleton().connect(_serverAddr,_serverPort);
			break;
		case Login_Connecting:
			break;
		case Login_Connect_OK:
			//Let user login
			setStatus(Login_Account_Requesting);
			break;
		case Login_Connect_Fail:
			NetManager::getSingleton().close();
			setStatus(Login_Select_Server);
			break;
		case Login_Account_Requesting:
			break;
		case Login_Account_Begin_Requesting:
			break;
		case Login_Account_OK:
		{
			//User account verified
			//Keep globals
			Globals.setInt32(Int32_LoginAreaId,_areaId);
			Globals.setInt32(Int32_LoginServerId,_serverId);
			Globals.setInt32(Int32_LoginMode,(int32)Login_First_Login);
			
			this->setActiveSceneId(Scene_CharacterSelect);
			break;
		}
		default:
			break;
		}

	}

	void LoginScene::activate()
	{
		GameScene::activate();
	}

	//Using a function table may better.(Note this table is sparse matrix)
	void LoginScene::buttonClicked(UIScreen* sender,UIControlID id)
	{
		UIScreen::Type type= sender->getType();
		if(type!=UIScreen::UIScreen_Login){
			return;
		}
		
		OnButtonClicked(id);

	}

	void LoginScene::OnButtonClicked(UIControlID id)
	{
		switch(id){
		case UIControlID_Alchemist:
			selectAlchemist();
			break;
		case UIControlID_Destroyer:
			selectDestroyer();
			break;
		case UIControlID_Vanquisher:
			selectVanquisher();
			break;

		case UIControlID_CreatePlayer:
			createPlayer();
			break;
		}

	}


	void LoginScene::selectAlchemist()
	{
		Unit* unit=getUnit("Alchemist");
		if(_activeUnit==unit){
			return;
		}

		if(_activeUnit){
			_activeUnit->setVisible(false);
		}
		_activeUnit=unit;
		if(_activeUnit){
			_activeUnit->setVisible(true);

		}

		resetCamera();
	}

	

	void LoginScene::selectDestroyer()
	{
		Unit* unit=getUnit("Destroyer");
		if(_activeUnit==unit){
			return;
		}
		if(_activeUnit){
			_activeUnit->setVisible(false);
		}
		_activeUnit=unit;
		if(_activeUnit){
			_activeUnit->setVisible(true);

		}

		resetCamera();

		
	}

	void LoginScene::selectVanquisher()
	{
		Unit* unit=getUnit("Vanquisher");
		if(_activeUnit==unit){
			return;
		}
		if(_activeUnit){
			_activeUnit->setVisible(false);
		}
		_activeUnit=unit;
		if(_activeUnit){
			_activeUnit->setVisible(true);

		}

		resetCamera();

	}

	void LoginScene::resetCamera()
	{
		PQEngine::CameraObject *camera=_sceneObject->getCameraObject();
		camera->setDirection(Ogre::Vector3(0,-0.2,-1));
		
		Ogre::Vector3 cameraPos=camera->getPositionOfFrameObject(_activeUnit->getMeshObject(),2.0f);
		camera->setPosition(cameraPos);
	}

	void LoginScene::createPlayer()
	{
		//should dispatch event ?
		setActiveSceneId(Scene_EnterWorld);

		
	}
}
